//
//  DESViewController.m
//  HappyBug
//
//  Created by liulei on 2018/12/29.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "DESViewController.h"
#import "Base64.h"
#import "AES.h"
#import "RSA.h"
@interface DESViewController ()

@end

#define RSAkey  @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDTbZ6cNH9PgdF60aQKveLz3FTalyzHQwbp601y77SzmGHX3F5NoVUZbdK7UMdoCLK4FBziTewYD9DWvAErXZo9BFuI96bAop8wfl1VkZyyHTcznxNJFGSQd/B70/ExMgMBpEwkAAdyUqIjIdVGh1FQK/4acwS39YXwbS+IlHsPSQIDAQAB"
@implementation DESViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(50, 50, 120, 40)];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}
-(void)click{
    //bsae64
    
    //使用Base64文件进行Base64加密和解密
    /*********************************使用Base64类*********************************/
    //使用Base64执行加密操作
    NSString *string = @"abcdefghijklmnopqrstuvwxyz";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSString *encodeString = [Base64 base64EncodedStringWithData:data];
    NSLog(@"encodeString : %@", encodeString);
    
    //使用Base64执行解密操作
    NSString *decodeString = nil;
    NSData *decodeData = [Base64 base64DecodeDataWithString:encodeString];
    decodeString = [[NSString alloc] initWithData:decodeData
                                         encoding:NSUTF8StringEncoding];
    NSLog(@"decodeString : %@", decodeString);
    
    /*********************************使用AES类*********************************/
    //使用AES执行加密操作
    NSString *aesKey = @"a1b2c3d4e5f6g7h8";
    //加密内容
    NSString *string3 = @"121";
    NSData *keyData3 = [aesKey dataUsingEncoding:NSUTF8StringEncoding];
    NSData *sourceData3 = [string3 dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encodeData3 = [AES encryptData:sourceData3 key:keyData3];
 //   NSLog(@"使用AES类加密 : %@", encodeData3);
    
    //使用AES执行解密操作
    NSString *decodeString3 = nil;
    NSData *decodeData3 = [AES decryptData:encodeData3
                                       key:keyData3];
    decodeString3 = [[NSString alloc] initWithData:decodeData3
                                          encoding:NSUTF8StringEncoding];
   // NSLog(@"使用AES类解密 : %@", decodeString3);
    
    /**************使用RSA类,手动生成der文件和key文件加密解密************************/
    //RSA
    //    //原始数据
    //    NSString *originalString = @"这是一段将要使用'.der'文件加密的字符串!";
    //
    //    //使用.der和.p12中的公钥私钥加密解密
    //    NSString *public_key_path = [[NSBundle mainBundle] pathForResource:@"public_key.der" ofType:nil];
    //    NSString *private_key_path = [[NSBundle mainBundle] pathForResource:@"private_key.p12" ofType:nil];
    //
    //    NSString *encryptStr = [RSA encryptString:originalString publicKeyWithContentsOfFile:public_key_path];
    //    NSLog(@"加密前:%@", originalString);
    //    NSLog(@"加密后:%@", encryptStr);
    //    NSLog(@"解密后:%@", [RSA decryptString:encryptStr privateKeyWithContentsOfFile:private_key_path password:@"4224"]);
    
    
    /**************使用RSA类,已经存在的秘钥加解密************************/
    //原始数据
    NSString *originalString = @"这是一段将要使用'秘钥字符串'进行加密的字符串!";
    
    //使用字符串格式的公钥私钥加密解密
    NSString *encryptStr = [RSA encryptString:originalString publicKey:RSAkey];
    
    NSLog(@"RSA加密前:%@", originalString);
    NSLog(@"RSA加密后:%@", encryptStr);
    NSLog(@"RSA解密后:%@", [RSA decryptString:encryptStr privateKey:@"MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANNtnpw0f0+B0XrRpAq94vPcVNqXLMdDBunrTXLvtLOYYdfcXk2hVRlt0rtQx2gIsrgUHOJN7BgP0Na8AStdmj0EW4j3psCinzB+XVWRnLIdNzOfE0kUZJB38HvT8TEyAwGkTCQAB3JSoiMh1UaHUVAr/hpzBLf1hfBtL4iUew9JAgMBAAECgYA1tGeQmAkqofga8XtwuxEWDoaDS9k0+EKeUoXGxzqoT/GyiihuIafjILFhoUA1ndf/yCQaG973sbTDhtfpMwqFNQq13+JAownslTjWgr7Hwf7qplYW92R7CU0v7wFfjqm1t/2FKU9JkHfaHfb7qqESMIbO/VMjER9o4tEx58uXDQJBAO0O4lnWDVjr1gN02cqvxPOtTY6DgFbQDeaAZF8obb6XqvCqGW/AVms3Bh8nVlUwdQ2K/xte8tHxjW9FtBQTLd8CQQDkUncO35gAqUF9Bhsdzrs7nO1J3VjLrM0ITrepqjqtVEvdXZc+1/UrkWVaIigWAXjQCVfmQzScdbznhYXPz5fXAkEAgB3KMRkhL4yNpmKRjhw+ih+ASeRCCSj6Sjfbhx4XaakYZmbXxnChg+JB+bZNz06YBFC5nLZM7y/n61o1f5/56wJBALw+ZVzE6ly5L34114uG04W9x0HcFgau7MiJphFjgUdAtd/H9xfgE4odMRPUD3q9Me9LlMYK6MiKpfm4c2+3dzcCQQC8y37NPgpNEkd9smMwPpSEjPW41aMlfcKvP4Da3z7G5bGlmuICrva9YDAiaAyDGGCK8LxC8K6HpKrFgYrXkRtt"]);
    
    NSLog(@"1");
    
    
}

@end
